package com.iw.adoms.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.iw.adoms.entity.Consumer;
import com.iw.adoms.entity.ConsumerInterview;
import com.iw.adoms.entity.Department;
import com.iw.adoms.entity.editor.EntityPropertyEditor;
import com.iw.adoms.service.ConsumerInterviewService;
import com.iw.adoms.service.ConsumerService;
import com.iw.adoms.service.DepartmentService;
import com.iw.core.orm.PageData;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/consumerInterview")
public class ConsumerInterviewAction {
	@Resource
	private ConsumerInterviewService consumerInterviewService;
	@Resource
	private ConsumerService consumerService;
	@Resource
	private DepartmentService departmentService;
	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Department.class, new EntityPropertyEditor(Department.class));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }		

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<ConsumerInterview> pageData = new PageData<ConsumerInterview>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = consumerInterviewService.find(pageData);

		return new ModelMap(pageData);
	}

	@RequestMapping(value = "/new")
	public String addForm(Model model) {
		ConsumerInterview consumerInterview = new ConsumerInterview();
		model.addAttribute(consumerInterview);
		
		List<Consumer> consumers = consumerService.findAll();
		model.addAttribute("consumers", consumers);
		
		List<Department> departments = departmentService.findAll();
		model.addAttribute("departments", departments);		
		
		return "/adoms/consumerInterview/input";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		ConsumerInterview consumerInterview = consumerInterviewService.find(id);
		model.addAttribute(consumerInterview);
		
		List<Consumer> consumers = consumerService.findAll();
		model.addAttribute("consumers", consumers);
		
		List<Department> departments = departmentService.findAll();
		model.addAttribute("departments", departments);	
		
		return "/adoms/consumerInterview/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(ConsumerInterview consumerInterview) {
		if (consumerInterview.isNew()) {
			consumerInterviewService.save(consumerInterview);
		} else {
			consumerInterviewService.update(consumerInterview);
		}

		return "redirect:/adoms/consumerInterview/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		consumerInterviewService.delete(id);
		return "redirect:/adoms/consumerInterview/list";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		consumerInterviewService.visible(id);
		return "redirect:/adoms/consumerInterview/list";// 重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		consumerInterviewService.unVisible(id);
		return "redirect:/adoms/consumerInterview/list";// 重定向
	}

}
